package net.dgg.zz.credential.externalFunction.service;

import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.credential.externalFunction.dao.ProductReservationDao;
import net.dgg.zz.credential.externalFunction.entity.ProductReservation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductReservationService {
    @Autowired
    private ProductReservationDao productReservationDao;

    public List<Map> queryDataWithPage(Map params) {
        return productReservationDao.queryDataWithPage(params);
    }


    /**
     * 根据证书code查询关联该证书的最早预约
     *
     * @param storageCapacityCode
     * @return
     */
    public List<ProductReservation> queryReservationByStorageCapacity(List<String> storageCapacityCode, String status) {
        List<ProductReservation> allProductReservation = null;
        ProductReservation productReservation = productReservationDao.queryReservationByEarliestStorageCapacity(storageCapacityCode, status);
        if (StringUtils.isNotEmpty(productReservation)) {
            allProductReservation = new ArrayList<>();
            Map params = new HashMap();
            params.put("scProductId", productReservation.getScProductId());
            params.put("status", status);
            List<ProductReservation> productReservations = productReservationDao.queryReservationByProduct(params);
            for (ProductReservation reservation : productReservations) {
                for (String s : storageCapacityCode) {
                    if (s.equals(reservation.getCertificateNo())) { // 如果该订单的预约证书code存在于指定证书code中
                        allProductReservation.add(reservation);
                    }
                }
            }
        }
        return allProductReservation;
    }

    /**
     * 修改证书状态
     *
     * @param productReservation
     */
    @Transactional
    public void updateReservation(ProductReservation productReservation) {
        productReservationDao.updateByPrimaryKey(productReservation);
    }

    /**
    * <p>@Description 通过生产订单查相关的预约</p>
    * <p>@author mqy</p>
    * <p>@Time 2018/8/28 0028 19:59 创建时间</p>
    * <p>@return</p>
    */
    public List<Map> queryListByScOrderId(Map params) {
        return productReservationDao.queryListByScOrderIdWithPage(params);
    }

}
